package com.pro01.backend.service;

import com.pro01.backend.mapper.Forum2PostLikeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class Forum2PostLikeService {

    @Autowired
    private Forum2PostLikeMapper likeMapper;

    // 点赞
    public int likePost(Long userId, Long postId) {
        if (likeMapper.hasLiked(userId, postId)) {
            throw new RuntimeException("您已点赞过该帖子");
        }

        int inserted = likeMapper.insertLike(userId, postId);
        if (inserted > 0) {
            likeMapper.incrementLikeCount(postId);
        }
        return inserted;
    }

    // 取消点赞
    public int unlikePost(Long userId, Long postId) {
        if (!likeMapper.hasLiked(userId, postId)) {
            throw new RuntimeException("您尚未点赞该帖子");
        }

        int deleted = likeMapper.deleteLike(userId, postId);
        if (deleted > 0) {
            likeMapper.decrementLikeCount(postId);
        }
        return deleted;
    }

}

